package com.ly.common.exception;

import com.ly.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 23:55 2024-06-21
 * @return
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {


    /**
     * 处理自定义的业务异常
     * @param e
     * @ExceptionHandler: 注解用来指明异常的处理类型，即如果这里指定为 NullPointerException，则数组越界异常就不会进到这个方法中来。
     * 拦截所有StateException异常
     */
    @ExceptionHandler (value = RuntimeException.class)
    public AjaxResult businessExceptionHandler (RuntimeException e) {
        log.error ( "发生异常 - {}", e.getMessage () );
        return AjaxResult.error ( e.getMessage () );
    }


    /**
     * 处理其他异常
     * @param e
     */
    @ExceptionHandler (value = NullPointerException.class)
    public AjaxResult nullPointerException (NullPointerException e) {
        log.error ( "未知异常 - [{}]", e );
        return AjaxResult.error ( "发生空指针异常，请联系管理员！" );
    }


    /**
     * 系统发生无法甄别的异常
     * @param e
     * @return
     */
    @ExceptionHandler (value = Exception.class)
    public AjaxResult exceptionHandler (Exception e) {
        log.error ( "未知异常 - [{}]", e );
        return AjaxResult.error ( e.getMessage () );
    }

}
